<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>


<div class="topo_layout" >
	<div  class="tree_panel">
		<ul id="featureTree" class="ztree"></ul>
	</div>
	<div class="detail_panel">
	</div>
</div>


<SCRIPT LANGUAGE="JavaScript">
	var treeObject;
	var selectNode;
	$(document).ready(
			function() {
				var setting = {
					url : "<s:url value='/feature/tree'/>", //获取节点数据的URL地址
					asyncParam : ["id"],
					async: {
					enable: true,
					url:"<s:url value='/feature/tree'/>",
					autoParam:["id"]
					},
					data: {
						key: {
							children: "nodes",
							name: "name",
						}
					},
					callback : {
						onClick : function(event, treeId, treeNode, msg) {
						selectNode = treeNode;
							$('.detail_panel').load('<s:url value="/feature/detail"/>?id=' + treeNode.id);
						},// 点击每个节点的事件:
						onAsyncSuccess : function(event, treeId, treeNode, msg) {
					
							treeObject = $.fn.zTree.getZTreeObj("featureTree");;
							//treeObject.expandAll(true);
							//var sNode = treeObject.getNodeByTId(selectNode1.tId);
							if(!selectNode){
								return;
							}
							selectNode = treeObject.getNodeByParam("id",selectNode.id, null);
							var pNode = selectNode.getParentNode();
							treeObject.expandNode(pNode, true, false, true);
			
							/**if(nodes.length>0){
								$('#detail_panel').load('<s:url value="/topo/detail"/>?id=' + nodes[0].id);
							}else{
								$('#detail_panel').load('<s:url value="/topo/detail"/>');
							}**/
							
							if(treeNode!=null){
						  		if(treeNode.nodes.length==0)
						     		treeNode.isParent =false;
						   		else
						     		treeNode.isParent =true;
						 		treeObject.updateNode(treeNode, true);
						   }

						}
					}
				};
				
			
				$.fn.zTree.init($("#featureTree"), setting);

				$("#add_button").bind('click',function(){
					$('#detail_panel').load('<s:url value="/feature/edit"/>?feature.parent.id='+treeObject.getSelectedNode().id+"&feature.parent.name="+encodeURIComponent(treeObject.getSelectedNode().name));
				})

				$("#delete_button").bind('click',function(){
					var selectedNode =	treeObject.getSelectedNode();
					$.tdqsAjax({
							type : "get",
							url : '<s:url value="/feature/delete"/>?id='+selectedNode.id,// 目标地址
							beforeSend : function() {
								
								
							},
							complete : function() {

							},
							error : function() {

							},
							success : function(){
								treeObject.reAsyncChildNodes(treeObject.getSelectedNode().parentNode, "refresh");		
								treeObject.selectNode(treeObject.getSelectedNode().parentNode);
								$('#detail_panel').load('<s:url value="/feature/detail"/>?id=' + treeObject.getSelectedNode().id);
								
								//TODO parent icon not be changed.
							}
					});
					
				})				
				   $(window).bind("resize",function(){

	               		var width= $('#layout').width()- $('#tree_panel').width()-7;

						
					
					});
				
			});
</script>
